import { Rule } from 'ant-design-vue/lib/form';
import { ref } from 'vue'

export function useConfirmPassword (_newPassword: string)  {
  //- 定义-新密码和确认密码响应式数据
  const newPasswordStrRef = ref<string>(_newPassword);
  const confirmPasswordStrRef = ref<string>("");

  //- 方法-验证确认密码
  const validateConfirmPassword = async (_rule: Rule, _value: string) => {
    // 1.判断是否为空值
    if (_value === "") {
      return Promise.reject("确认密码不能为空");
    }
    // 2.判断是否与新密码一致
    if (_value !== newPasswordStrRef.value) {
      return Promise.reject("两次密码不一致");
    }
    return Promise.resolve();
  };

  //- 确认密码校验规则
  const confirmPasswordRulesArr: Array<Object> = [
    { required: true, validator: validateConfirmPassword }
  ];

  //- 返回暴露数据
  return {
    confirmPasswordStrRef,
    confirmPasswordRulesArr,
  }
}